PIC24F
SPI Peripheral Module Library Help
Table Of Contents
2 Using Library Functions in Your Code
3.2 DataRdySPIx1 , DataRdySPI2
3.3 ConfigIntSPI1 , ConfigIntSPI2
4.1 EnableIntSPI1 , EnableIntSPI2
4.2 DisableIntSPI1 , DisableIntSPI2
4.3 SetPriorityIntSPI1 , SetPriorityIntSPI2
This peripheral library module:
· Supports BYTE and WORD transmission and reception.
· Incorporates multiple BYTE and WORD transmission and reception in a single function call.
· Provides simple functions to read from and write to the buffers.
· Provides simple interface macros to enable/disable interrupts.
Library routine parameters can be constructed using either AND based mask or AND_OR based mask setting. For more information on these masks, see 16-bit Peripheral Libraries.
Examples of use for both the methods are below.
Example of Use ( AND mask )
#include<spi.h>
/* Data received at SPI2 */
unsigned int datard ;
void __attribute__((__interrupt__)) _SPI1Interrupt(void)
{
IFS0bits.SPI1IF = 0;
}
void __attribute__((__interrupt__)) _SPI2Interrupt(void)
{
IFS2bits.SPI2IF = 0;
SPI1STATbits.SPIROV = 0; /* Clear SPI1 receive overflow
flag if set */
}
int main(void)
{
/* Holds the information about SPI configuration */
unsigned int SPICON1Value;
/* Holds the information about SPI configuration */
unsigned int SPICON2Value;
/* Holds the information about SPI Enable/Disable */
unsigned int SPISTATValue;
/*Timeout value during which timer1 is ON */
int timeout;
/* Turn off SPI modules */
CloseSPI1();
CloseSPI2();
TMR1 = 0 ;
timeout = 0;
TRISDbits.TRISD0 = 0;
/* Configure SPI2 interrupt */
ConfigIntSPI2(SPI_INT_EN & SPI_INT_PRI_6);
/* Configure SPI1 module to transmit 16 bit timer1 value
in master mode */
SPICON1Value = ENABLE_SCK_PIN & ENABLE_SDO_PIN & SPI_MODE16_ON &
SPI_SMP_ON & SPI_CKE_OFF &
SLAVE_ENABLE_OFF &
CLK_POL_ACTIVE_HIGH &
MASTER_ENABLE_ON &
SEC_PRESCAL_7_1 &
PRI_PRESCAL_64_1;
SPICON2Value = FRAME_ENABLE_OFF & FRAME_SYNC_OUTPUT;
SPISTATValue = SPI_ENABLE & SPI_IDLE_CON &
SPI_RX_OVFLOW_CLR;
OpenSPI1(SPICON1Value,SPICON2Value,SPISTATValue );
/* Configure SPI2 module to receive 16 bit timer value in
slave mode */
SPICON1Value = ENABLE_SCK_PIN & ENABLE_SDO_PIN & SPI_MODE16_ON &
SPI_SMP_OFF & SPI_CKE_OFF &
SLAVE_ENABLE_OFF &
CLK_POL_ACTIVE_HIGH &
MASTER_ENABLE_OFF &
SEC_PRESCAL_7_1 &
PRI_PRESCAL_64_1;
SPICON2Value = FRAME_ENABLE_OFF & FRAME_SYNC_OUTPUT;
SPISTATValue = SPI_ENABLE & SPI_IDLE_CON &
SPI_RX_OVFLOW_CLR;
OpenSPI1(SPICON1Value,SPICON2Value,SPISTATValue );
T1CON = 0X8000;
while(timeout< 100 )
{
timeout = timeout+2 ;
}
T1CON = 0;
WriteSPI1(TMR1);
while(SPI1STATbits.SPITBF);
while(!DataRdySPI2());
datard = ReadSPI2();
if(datard <= 600)
{
PORTDbits.RD0 = 1;
}
/* Turn off SPI module and clear IF bit */
CloseSPI1();
CloseSPI2();
return 0;
}
Example of Use ( AND_OR mask )'
#define USE_AND_OR /* To enable AND_OR mask setting */
#include<spi.h>
/* Data received at SPI2 */
unsigned int datard ;
void __attribute__((__interrupt__)) _SPI1Interrupt(void)
{
IFS0bits.SPI1IF = 0;
}
void __attribute__((__interrupt__)) _SPI2Interrupt(void)
{
IFS1bits.SPI2IF = 0;
SPI1STATbits.SPIROV = 0; /* Clear SPI1 receive overflow
flag if set */
}
int main(void)
{
/* Holds the information about SPI configuartion */
unsigned int SPICON1Value;
/* Holds the information about SPI configuartion */
unsigned int SPICON2Value;
/* Holds the information about SPI Enable/Disable */
unsigned int SPISTATValue;
/*Timeout value during which timer1 is ON */
int timeout;
/* Turn off SPI modules */
CloseSPI1();
CloseSPI2();
TMR1 = 0 ;
timeout = 0;
TRISDbits.TRISD0 = 0;
/* Configure SPI2 interrupt */
ConfigIntSPI2(SPI_INT_EN | SPI_INT_PRI_6);
/* Configure SPI1 module to transmit 16 bit timer1 value
in master mode */
SPICON1Value = ENABLE_SCK_PIN | ENABLE_SDO_PIN | SPI_MODE16_ON |
SPI_SMP_ON | SPI_CKE_OFF |
SLAVE_ENABLE_OFF |
CLK_POL_ACTIVE_HIGH |
MASTER_ENABLE_ON |
SEC_PRESCAL_7_1 |
PRI_PRESCAL_64_1;
SPICON2Value = FRAME_ENABLE_OFF;
SPISTATValue = SPI_ENABLE | SPI_IDLE_CON |
SPI_RX_OVFLOW_CLR;
OpenSPI1(SPICON1Value,SPICON2Value,SPISTATValue );
/* Configure SPI2 module to receive 16 bit timer value in
slave mode */
SPICON1Value = ENABLE_SCK_PIN | ENABLE_SDO_PIN | SPI_MODE16_ON |
SPI_SMP_OFF | SPI_CKE_OFF |
SLAVE_ENABLE_OFF |
CLK_POL_ACTIVE_HIGH |
MASTER_ENABLE_OFF |
SEC_PRESCAL_7_1 |
PRI_PRESCAL_64_1;
SPICON2Value = FRAME_ENABLE_OFF;
SPISTATValue = SPI_ENABLE | SPI_IDLE_CON |
SPI_RX_OVFLOW_CLR;
OpenSPI2(SPICON1Value,SPICON2Value,SPISTATValue );
T1CON = 0X8000;
while(timeout< 100 )
{
timeout = timeout+2 ;
}
T1CON = 0;
WriteSPI1(TMR1);
while(SPI1STATbits.SPITBF);
while(!DataRdySPI2());
datard = ReadSPI2();
if(datard <= 600)
{
PORTDbits.RD0 = 1;
}
/* Turn off SPI module and clear IF bit */
CloseSPI1();
CloseSPI2();
return 0;
}
Function Prototype |
void CloseSPI1(void); void CloseSPI2(void); |
Include |
spi.h |
Description |
This function turns off the SPI module |
Arguments |
None |
Return Value |
None |
Remarks: |
This function disables the SPI interrupt and then turns off the module. The Interrupt Flag bit is also cleared |
Function Prototype |
char DataRdySPI1(void); char DataRdySPI2(void); |
Include |
spi.h |
Description |
This function determines if the SPI buffer contains any data to be read. |
Arguments |
None |
Return Value |
If 1 is returned, it indicates that the data has been received in the receive buffer and is to be read. If 0 is returned, it indicates that the receive is not complete and the receive buffer is empty. |
Remarks: |
This function returns the status of SPI receive buffer. This indicates if the SPI receive buffer contains any new data that is yet to be read as indicated by the SPIxSTAT<SPIRBF> bit. This bit is cleared by hardware when the data is read from the buffer. |
Function Prototype |
void ConfigIntSPI1(unsigned int config); void ConfigIntSPI2(unsigned int config); |
Include |
spi.h |
Description |
This function configures the SPI Interrupt |
Arguments |
config - SPI interrupt priority and enable/disable information as defined below: Interrupt enable/disable SPI_INT_EN SPI_INT_DIS Interrupt Priority SPI_INT_PRI_0 SPI_INT_PRI_1 SPI_INT_PRI_2 SPI_INT_PRI_3 SPI_INT_PRI_4 SPI_INT_PRI_5 SPI_INT_PRI_6 SPI_INT_PRI_7 |
Return Value |
None |
Remarks: |
This function clears the Interrupt Flag bit, sets the interrupt priority and enables/disables the interrupt |
Function Prototype |
unsigned int ReadSPI1(void); unsigned int ReadSPI2(void); |
Include |
spi.h |
Description |
This function reads the content of the SPI Receive Buffer (SPIxBUF) register. |
Arguments |
None |
Return Value |
This function returns the content of Receive Buffer (SPIxBUF) register. If a value of -1 is returned, it indicates that there is no data to be read from the SPI buffer. |
Remarks: |
This function returns the content of the Receive Buffer register. If 16-bit communication is enabled, the data in the SPIxRBF register is returned. If 8-bit communication is enabled, then the lower byte of SPIxBUF is returned. The SPIxBUF is read only if it contains any data as indicated by the SPISTAT<RBF>bit. Otherwise, a value of -1 is returned. |
Function Prototype |
void WriteSPI1(unsigned int data); void WriteSPI2(unsigned int data); |
Include |
spi.h |
Description |
This function writes the data to be transmitted into the Transmit Buffer (SPIxBUF) register. |
Arguments |
data - This is the data to be transmitted which will be stored in SPI buffer. |
Return Value |
None |
Remarks: |
This function writes the data (byte/word) to be transmitted into the transmit buffer. If 16-bit communication is enabled, the 16-bit value is written to the transmit buffer. If 8-bit communication is enabled, then upper byte is masked and then written to the transmit buffer. |
Function Prototype |
void OpenSPI1(unsigned int config1, unsigned int config2); void OpenSPI2(unsigned int config1, unsigned int config2); |
Include |
spi.h |
Description |
This function configures the SPI module |
Arguments |
config1 - This contains the parameters to be configured in the SPIxCON1 register as defined below: SCK Pin Control bit DISABLE_SCK_PIN ENABLE_SCK_PIN SDO Pin Control bit DISABLE_SDO_PIN ENABLE_SDO_PIN Word/Byte Communication mode SPI_MODE16_ON SPI_MODE16_OFF SPI Data Input Sample phase SPI_SMP_ON SPI_SMP_OFF SPI Clock Edge Select SPI_CKE_ON SPI_CKE_OFF SPI slave select enable SLAVE_SELECT_ENABLE_ON SLAVE_SELECT_ENABLE_OFF SPI Clock polarity select CLK_POL_ACTIVE_LOW CLK_POL_ACTIVE_HIGH SPI Mode Select bit MASTER_ENABLE_ON MASTER_ENABLE_OFF Secondary Prescale select SEC_PRESCAL_1_1 SEC_PRESCAL_2_1 SEC_PRESCAL_3_1 SEC_PRESCAL_4_1 SEC_PRESCAL_5_1 SEC_PRESCAL_6_1 SEC_PRESCAL_7_1 SEC_PRESCAL_8_1 Primary Prescale select PRI_PRESCAL_1_1 PRI_PRESCAL_4_1 PRI_PRESCAL_16_1 PRI_PRESCAL_64_1
config2 - This contains the parameters to be configured in the SPIxCON2 register as defined below: Framed SPI support Enable/Disable FRAME_ENABLE_ON FRAME_ENABLE_OFF Frame Sync Pulse direction control FRAME_SYNC_INPUT FRAME_SYNC_OUTPUT Frame Sync Pulse Polarity control FRAME_SYNC_ACTIVE_HIGH FRAME_SYNC_ACTIVE_LOW Frame Sync Pulse Edge select SPI_FRM_PULSE_FIRST_CLK SPI_FRM_PULSE_PREV_CLK Enhanced Buffer Mode Bit SPI_ENH_BUFF_ENABLE SPI_ENH_BUFF_DISABLE
Config3 - This contains the parameters to be configured in the SPIxSTAT register as defined below: SPI Enable/Disable SPI_ENABLE SPI_DISABLE SPI Idle mode operation SPI_IDLE_CON SPI_IDLE_STOP Clear Receive Overflow Flag bit SPI_RX_OVFLOW_CLR |
Return Value |
None |
Remarks: |
This functions initializes the SPI module and sets the Idle mode Operation. |
Function Prototype |
void putsSPI1(unsigned int length, unsigned int *wrptr); void putsSPI2(unsigned int length, unsigned int *wrptr); |
Include |
spi.h |
Description |
This function writes the data to be transmitted into the Transmit Buffer (SPIxBUF) register. |
Arguments |
length - the number of data words/bytes to be transmitted. wrptr - the pointer to the string of data to be transmitted. |
Return Value |
None |
Remarks: |
This function writes the specified length of data words/bytes to be transmitted into the transmit buffer. Once the transmit buffer is full, it waits until the data gets transmitted and then writes the next data into the Transmit register. The control remains in this function if SPI module is disabled while SPITBF bit is set. |
Function Prototype |
unsigned int getsSPI1(unsigned int length, unsigned int *rdptr, unsigned int spi_data_wait); unsigned int getsSPI2(unsigned int length, unsigned int *rdptr,unsigned int spi_data_wait); |
Include |
spi.h |
Description |
This function reads a string of data of specified length and stores it into the location specified. |
Arguments |
length - the length of the string to be received. rdptr - the pointer to the location where the data received have to be stored. spi_data_wait - the time-out count for which the module has to wait before return. If the time-out count is N, the actual time out would be about (19 * N 1) instruction cycles. |
Return Value |
This function returns the number of bytes yet to be received. If the return value is a 0, it indicates that the complete string has been received. If the return value is a non-zero, it indicates that the complete string has not been received |
Remarks: |
None |
Include |
spi.h |
Description |
This function is identical to ReadSPI1 and ReadSPI2, i.e., #define to ReadSPI1 and ReadSPI2 in spi.h. |
Include |
spi.h |
Description |
This function is identical to WriteSPI1 and WriteSPI2, i.e., #define to WriteSPI1 and WriteSPI2 in spi.h. |
Macro |
EnableIntSPI1 EnableIntSPI2 |
Include |
spi.h |
Description |
Enables SPIx Transfer Complete Interrupt. |
Arguments |
None |
Remarks |
This macro sets SPI Interrupt Enable bit of Interrupt Enable Control register. |
Macro |
DisableIntSPI1 DisableIntSPI2 |
Include |
spi.h |
Description |
Disables the SPI interrupt. |
Arguments |
None |
Remarks |
This macro clears SPI Interrupt Enable bit of Interrupt Enable Control register. |
Macro |
SetPriorityIntSPI1 SetPriorityIntSPI2 |
Include |
spi.h |
Description |
Sets the priority level for SPI interrupt. |
Arguments |
priority - This input parameter is the level of interrupt priority. |
Remarks |
None |